using UnityEngine;
using UnityEngine.UI;
using static WaterCylinder;

/*
小文本显示，用于显示伤害，显示小对话之类
*/

public class SmallText : MonoBehaviour
{   
    public static SmallText CreatSmallText(string str, Vector2 pos){
        GameObject obj = Creat(GetPrefab("DefaultObject/SmallText"), pos);
        SmallText t = obj.GetComponent<SmallText>();
        t.text = str;
        return t;
    }
    [SerializeField]
    private Text _text;
    public string text;
    //流程控制变量
    public int A;
    
    //移动
    Vector2 Move(float x, float y, bool smooth = false, float speed = 2f){
        Vector2 pos = transform.position;
        if(smooth){
            pos.x = Mathf.Lerp(pos.x, x, 1-1/speed);
            pos.y = Mathf.Lerp(pos.y, y, 1-1/speed);
        }else{
            pos.x += x;
            pos.y += y;
        }
        return transform.position = pos;
    }
    
    //改变大小
    float Scale(float s, bool smooth = false, float speed = 0.3f){
        Vector2 scale = transform.localScale;
        if(smooth){
            scale.x = Mathf.Lerp(scale.x, s, speed);
            scale.y = Mathf.Lerp(scale.y, s, speed);
        }else{
            scale.x += s;
            scale.y += s;
        }
        transform.localScale = scale;
        return scale.x;
    }
    
    //透明度
    float Alphy(float a){
        Color c = _text.color;
        c.a += a;
        _text.color = c;
        return c.a;
    }
    
    float time = 0;
    bool ForTime(float limit){
        time += Time.deltaTime;
        if(time >= limit){
            time = 0;
            return true;
        }
        else return false;
    }
    
    //移动方式，修改以决定如何显示该文本
    void Movement(){
        switch(A){
            case 0:
                Move(0, Time.deltaTime);
                if(Approach(Scale(1, true), 1))A++;
                break;
            case 1:
                if(ForTime(1f))A++;
                break;
            case 2:
                if(Approach(Scale(0, true), 0))A++;
                break;
            case 3:
                Disappear();
                break;
        }
    }

    //消失方式，修改以决定文本以何种方式消失
    void Disappear(){
        Delete(gameObject);
    }

    void Update(){
        Movement();
    }
    
    void Start(){
        Scale(-1);
        _text.text = text;
        A = 0;
    }
}
